package com.qyhl.module_activities.act.comment;


import com.qyhl.webtv.commonlib.entity.act.ActCommentBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Created by helin on 2018/1/22.
 */

public class CommentListPresenter implements CommentListContract.CommentListPresenter {

    private final CommentListContract.CommentListView view;
    private final CommentListModel model;
    private float lastTime;

    CommentListPresenter(CommentListContract.CommentListView view) {
        this.view = view;
        this.model = new CommentListModel(this);
    }

    @Override
    public void commentError(String msg) {
        view.commentError(msg);
    }

    @Override
    public void commentSuccess(boolean isshow,String msg) {
        view.commentSuccess(isshow,msg);
    }

    @Override
    public void getCommentError(String msg, boolean isHead) {
        view.getCommentError(msg, isHead);
    }

    @Override
    public void onSuccess(List<ActCommentBean> data, boolean isHead) {
        if (data == null || data.size() == 0) {
            view.onEmpty(isHead);
        } else {
            setCommentTime(data);
            if (getLastTime(isHead, data)) {
                view.onSuccess(data, isHead);
            }
        }
    }

    /**
     * 判断获取到的数据是否在之前的数据之后，如果时间小于上一次最后一条时间，说明数据重复
     */
    private boolean getLastTime(boolean isHead, List<ActCommentBean> data) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            if (isHead) {
                lastTime = simpleDateFormat.parse(data.get(data.size() - 1).getCommentTime()).getTime();
            } else {
                long nowOneTime = simpleDateFormat.parse(data.get(0).getCommentTime()).getTime();
                if (nowOneTime >= lastTime) {
                    lastTime = simpleDateFormat.parse(data.get(data.size() - 1).getCommentTime()).getTime();
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void getComment(int actId, int commentId) {
        model.getComment(actId, commentId);
    }

    @Override
    public void sendComment(int actId, String content) {
        model.sendComment(actId, content);
    }

    private void setCommentTime(List<ActCommentBean> data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.US);
        SimpleDateFormat mouthFormat = new SimpleDateFormat("MM-dd", Locale.US);
        SimpleDateFormat beyondyear = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        long curTime = System.currentTimeMillis();

        for (ActCommentBean bean : data) {
            String commentTime = bean.getCommentTime();
            try {
                long tDate = simpleDateFormat.parse(commentTime).getTime();
                if (tDate > curTime) {
                    bean.setCommentTime("刚刚");
                } else if (yearFormat.format(tDate).equals(yearFormat.format(curTime))) {
                    long dtime = (curTime - tDate) / 1000;
                    if (dtime <= 60L) {
                        if (dtime == 0) {
                            bean.setCommentTime("刚刚");
                        } else {
                            bean.setCommentTime(dtime + "秒前");
                        }
                    } else if (dtime <= 3600L) {
                        if (mouthFormat.format(curTime).equals(mouthFormat.format(tDate))) {
                            //判断同一天
                            bean.setCommentTime(dtime / 60 + "分钟前");
                        } else {
                            bean.setCommentTime(mouthFormat.format(tDate));
                        }
                    } else if (dtime <= 86400L) {
                        if (mouthFormat.format(curTime).equals(mouthFormat.format(tDate))) {
                            //判断同一天
                            bean.setCommentTime(dtime / 3600 + "小时前");
                        } else {
                            bean.setCommentTime(mouthFormat.format(tDate));
                        }
                    } else {
                        bean.setCommentTime(mouthFormat.format(tDate));
                    }
                } else {
                    bean.setCommentTime(beyondyear.format(tDate));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
